import time
from pprint import pprint

from selenium.webdriver.support.wait import WebDriverWait
from chrome import get_webdriver
from utils import load_config, create_dir
from base import login
from vpp import resource_archives, dataStatistics
from ariot import summary, alldevice, groupdevice
from ktjk import ktjk
from log import logger

def job_hvpp():
    # 载入配置文件
    conf = load_config()

    timeout = conf.get('web_settings').get('timeout')
    poll_frequency = conf.get('web_settings').get('poll_frequency')
    # 创建文件夹
    file_path = create_dir()

    driver = get_webdriver()
    web = WebDriverWait(driver, timeout, poll_frequency)

    try:
        # hvpp登录
        login(driver, web, **conf['base_info'])

        # 1、已并网项目总量
        conf['steps']['resource_archives'].update(conf['base_info'])
        pprint(conf['steps']['resource_archives'])
        resource_archives(driver, web, **conf['steps']['resource_archives'])

        # 2、已并网项目数据采集状态
        conf['steps']['dataStatistics'].update(conf['base_info'])
        pprint(conf['steps']['dataStatistics'])
        dataStatistics(driver, web, **conf['steps']['dataStatistics'])

        # 设备总数
        conf['steps']['summary'].update(conf['base_info'])
        pprint(conf['steps']['summary'])
        summary(driver, web, **conf['steps']['summary'])

        # 设备管理 负控单元数量统计
        conf['steps']['alldevice'].update(conf['base_info'])
        pprint(conf['steps']['alldevice'])
        alldevice(driver, web, **conf['steps']['alldevice'])

        # 分组管理
        conf['steps']['groupdevice'].update(conf['base_info'])
        pprint(conf['steps']['groupdevice'])
        groupdevice(driver, web, **conf['steps']['groupdevice'])

        # 空调集控系统
        conf['steps']['ktjk'].update(conf['base_info'])
        pprint(conf['steps']['ktjk'])
        ktjk(driver, web, **conf['steps']['ktjk'])
    except Exception as e:
        logger.exception(e)
    finally:
        driver.close()

    # # 获取执行步骤
    # jobs = conf.get('steps')
    # for job in jobs:
    #     for k, v in job.items():
    #         path = v['path']
    #         data = v['data']
    #         url = urljoin(base_url, path)
    #         kwargs = {
    #             'url': url,
    #             'data': data,
    #             'file_path': file_path
    #         }
    #         func = k
    #         print(kwargs)
    #         # 变量转函数
    #         result = eval(func + '(driver, web, **kwargs)')
    #         print(result)
    #
    # driver.close()


#

if __name__ == '__main__':
    job_hvpp()
